﻿using CloudStorage_Backend.dao;
using CloudStorage_Backend.exception;
using CloudStorage_Backend.model;
using CloudStorage_Backend.service;
using CloudStorage_Backend.utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CloudStorage_Backend.controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly UserService userService;

        public UserController(UserService service)
        {
            this.userService = service;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public ResponseResult<string> Login(User user)
        {
             User u = userService.Login(user);
            return ResponseResult<string>.Ok(JwtHelper.GetToken(u));
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("register")]
        public ResponseResult<User> Register(User user)
        {
            userService.Register(user);
            return ResponseResult<User>.Ok(user);
        }
    }
}
